
//
// 130424 - AH - Created.
//

package org.japril.swing.jutil.xmloperations;

import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import org.japril.jutil.xmloperations.AbstractXmlOperation;
import org.japril.jutil.xmloperations.XmlOperation;

/**
 * <p>
 *   {@link TableModel} that adapts a
 *   {@link XmlOperation}'s XPath paths.
 * </p>
 * @author Arne Halvorsen (AH)
 */
@SuppressWarnings("serial")
public final class XmlOperationPathTableModel extends AbstractTableModel
{
  private final XmlOperation m_oper;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public XmlOperationPathTableModel(){ this(null); }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param oper Operation to adapt.
   */
  public XmlOperationPathTableModel(XmlOperation oper)
  {
    m_oper=oper;
  }

  // Specified in TableModel.
  public int getColumnCount(){ return 1; }

  // Specified in TableModel.
  public int getRowCount()
  {
    return m_oper==null ? 0 : m_oper.getPathCount();
  }

  // Specified in TableModel.
  public Object getValueAt(int row, int col)
  {
    if (m_oper==null)
    {
      throw new IndexOutOfBoundsException(Integer.toString(row));
    }
    if (col!=0)
    {
      throw new IndexOutOfBoundsException(Integer.toString(col));
    }
    
    
    return m_oper.getPath(row);
  }

  // Specified in TableModel.
  public Class<?> getColumnClass(int col)
  {
    if (col!=0)
    {
      throw new IndexOutOfBoundsException(Integer.toString(col));
    }
    
    return String.class;
  }

  // Specified in TableModel.
  public String getColumnName(int col) 
  {
    if (col!=0)
    {
      throw new IndexOutOfBoundsException(Integer.toString(col));
    }
    
    return "XPath";
  }
  
  /**
   * <p>
   *   Adds path.
   * </p>
   * @param path Path to add.
   * @throws IllegalArgumentException If can not add path.
   */
  public void addPath(String path)
  {
    if (m_oper==null)
    {
      throw new IllegalArgumentException("has no operation to add path to");
    }
    
    if (m_oper instanceof AbstractXmlOperation)
    {
      int n=m_oper.getPathCount();
      ((AbstractXmlOperation)m_oper).addPath(path);
      fireTableRowsInserted(n, n);
      return;
    }
    
    throw new IllegalArgumentException("can not add to a "+m_oper.getClass());
  }
  
  /**
   * <p>
   *   Remove path.
   * </p>
   * @param idx Index of path to remove.
   * @throws IllegalArgumentException If can not remove path.
   * @throws IndexOutOfBoundsException If {@code idx} out of bounds.
   */
  public void removePath(int idx)
  {
    if (m_oper==null)
    {
      throw new IllegalArgumentException("has no operation to remove path to");
    }
    
    if (m_oper instanceof AbstractXmlOperation)
    {
      ((AbstractXmlOperation)m_oper).removePath(idx);
      fireTableRowsInserted(idx, idx);
    }
  }
  
}
